package cn.controller;

/**
 * Created by wsx on 2016/12/30.
 */
import cn.model.User;
import cn.service.EmailService;
import cn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void checklogininfo(@RequestParam("uemail") String uemail, @RequestParam("upasswd") String upasswd, HttpSession session, HttpServletResponse response)
    {
        try {
            String ret = userService.login(uemail,upasswd);
            if(ret.equals("true")){
                session.setAttribute("uemail",uemail);
                response.getWriter().print("true");
            }else{
                response.getWriter().print("false");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public void logout(HttpSession session, HttpServletResponse response) {
        try {
            session.removeAttribute("uemail");
            response.getWriter().print("true");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public void register(HttpServletResponse response, User user) {
        try {
            response.getWriter().print(userService.register(user));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/loginpage",method = RequestMethod.GET)
    public String loginpage()
    {
        return "login";
    }

    @RequestMapping(value = "/registerpage",method = RequestMethod.GET)
    public String registerpage()
    {
        return "register";
    }
}
